package com.xplay.xpocker.room.mahjong.action;

import com.xplay.xpocker.constant.MessageNoticeEnum;
import com.xplay.xpocker.dvo.GameRuMahjongUserState;
import com.xplay.xpocker.entity.GameRuMahjongLogs;
import com.xplay.xpocker.room.RoomInfoTL;
import com.xplay.xpocker.room.mahjong.AbstractMahjongMessageStrategy;
import com.xplay.xpocker.room.mahjong.AbstractIsolationEventStrategy;
import com.xplay.xpocker.room.mahjong.constant.MKey;
import com.xplay.xpocker.room.mahjong.dto.MahjongActionEnum;
import com.xplay.xpocker.room.mahjong.dto.MahjongMessage;
import com.xplay.xpocker.room.mahjong.dto.MahjongRoomInfo;
import com.xplay.xpocker.room.mahjong.dto.MahjongRoomRule;
import com.xplay.xpocker.room.message.MessageContent;
import com.xplay.xpocker.room.message.MessageContentFactory;
import com.xplay.xpocker.room.message.MessageToClient;
import com.xplay.xpocker.room.message.MessageType;
import com.xplay.xpocker.dvo.AbstractRoom;
import com.xplay.xpocker.util.Assertion;
import com.xplay.xpocker.util.JacksonStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ExportCardAction extends AbstractMahjongMessageStrategy<AbstractRoom<MahjongRoomRule, MahjongRoomInfo, GameRuMahjongLogs>, MahjongMessage> {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    protected String strategyName() {
        return MahjongActionEnum.EXPORT_CARD.getAction();
    }

    @Override
    public boolean doMessage(AbstractRoom<MahjongRoomRule, MahjongRoomInfo, GameRuMahjongLogs> roomInfo, MahjongMessage messageInfo) {
        MahjongRoomInfo gameInfo = roomInfo.getGameInfo();
        // 摸牌的话  只有摸的那个玩家  才能看到牌 其余玩家不能
        GameRuMahjongUserState currentUser = gameInfo.getCurrentUser();
        // 判断用户是否存在听牌
        Assertion.isTrue(checkExistsWaitCompleted(MahjongActionEnum.CALL), "等待桌面上其他玩家回应");
        log.debug("房间号:{},用户：{},出牌：{}", gameInfo.getCode(), currentUser.getUsername(), messageInfo.getCardCode());


        List<MessageContent<MessageToClient>> exportMessage = MessageContentFactory.
                init(roomInfo.getCode(), MessageType.ACTION.getType(),
                        currentUser.getUserId(), MessageNoticeEnum.ALL)
                .multi(MKey.exeActionUid, currentUser.getUserId(),
                        MKey.type, MahjongActionEnum.EXPORT_CARD.getAction(),
                        MKey.cardCode, messageInfo.getCardCode()
                )
                .send().getMessageList();
        RoomInfoTL.addMessageToClient(exportMessage);

        Integer exportCardCode = messageInfo.getCardCode();
        Assertion.isTrue(exportCardCode == null || !currentUser.allHandCards().contains(exportCardCode), "非法的出牌事件");
        gameInfo.setExportCard(exportCardCode);
        gameInfo.setExportUserId(currentUser.getUserId());
        gameInfo.setExportUserSeq(currentUser.getSeq());
        // 如果没有人处理的话 接着应该是用户摸牌事件
        // 移出玩家的手牌
        currentUser.removeCard(exportCardCode);
        //TODO  设置出牌信息,调试期间主动关闭
        currentUser.setExportCards(currentUser.addExportCard(exportCardCode));
        currentUser.setTouchCard(null);

        // 换牌后 不同类型的 需要做不同类型的处理
        boolean loading = AbstractIsolationEventStrategy.getSettlementStrategy(roomInfo.getType())
                .exportCardActionCheck(currentUser, exportCardCode, roomInfo);

        // 如果没有操作 就直接发牌 默认的发牌操作
        if (!loading) {
            dealCard(null, true);
        }
        return true;

    }

}
